<?php
/**
 * Created by PhpStorm.
 * User: WX-C
 * Date: 2018/4/19
 * Time: 15:40
 */

namespace app\admin\controller;


use app\common\helper\Response;
use app\common\model\Form;
use app\common\model\Text;
use think\Db;
use think\Hook;

/**
 * Class Questionnaire
 * @package app\admin\controller
 * @property \app\common\model\Questionnaire $model
 */
class Questionnaire extends Base
{
    public function _initialize()
    {
        $this->model=new \app\common\model\Questionnaire();
        parent::_initialize(); // TODO: Change the autogenerated stub
    }
    public function index(){
        if($this->request->isAjax()){
            $questionnaires=$this->model->index();
            $questionnaires=collection($questionnaires)->each(function($v){
                return $v['data']=count($v->question());
            });
            return Response::success($questionnaires);
        }
        return $this->fetch();
    }
    public function add(){
        if($this->request->isPost()){
            $data=input();
            $r=$this->model->add($data);
            if($r===false){
                return $this->error('添加问卷失败');
            }
            return $this->success('添加问卷成功','questionnaire/index');
        }
        return $this->fetch();
    }
    public function edit(){
        if($this->request->isPost()){
            $data=input();
            $data=array_filter($data);
            $r=$this->model->edit($data);
            if($r===false){
                return   Response::error("编辑失败");
            }
            return   Response::success('','',"编辑成功");
        }
        $id=input('id');
        $list=$this->model->idFind($id);
        $this->assign('list',$list);
        $question=$this->model->idQuestion($list['qids']);
        $this->assign('question',$question);
        return $this->fetch();
    }

    public function detail(){
        $id=input('id');
        $list=$this->model->idFind($id);
        $this->assign('list',$list);
        $name=Db::table('questionnaire')->find($id);
        $this->assign('name',$name);
        $question=$this->model->idQuestions($id);
        $this->assign('question',$question);
        return $this->fetch();
    }

    public function show(){
        $id=input('id',0,'intval');
        $naire=$this->model->where(['id'=>$id])
            ->find();
        $html='';
        $form=new Form();
        $html.=$form->open();
        $html.=$form->hidden('id',$id);
        $questions=$naire->question();
        foreach ( $naire->qids as $qid){
            $q=$questions[$qid];
            $q['ext']=json_decode($q['ext'],true);
            switch ($q['type']){
                case 'radio':
                    $html.=$form->radio($q['name'],md5($q['id']),$q['ext']);
                    break;
                case 'checkbox':
                    $html.=$form->checkbox($q['name'],md5($q['id']),$q['ext']);
                    break;
                case 'textarea':
                    $html.=$form->textarea($q['name'],md5($q['id']));
                    break;
                case 'table':
                    $html.=$form->table($q['name'],md5($q['id']));
            }
        }
        $this->assign('form',$html);
        return $this->fetch('show');
    }
    public function del(){
        if($this->request->isPost()){
            $id=input('id');
            $r=$this->model->del($id);
            if($r===false){
               return json(['code'=>1,'msg'=>"删除失败"]);
            }
            return json(['code'=>200,'msg'=>"删除成功"]);
        }
    }
    public function sort(){
        if($this->request->isPost()){
            $data=input();
            $r=$this->model->edit($data);
            if($r===false){
                return  Response::error("编辑失败");
            }
            return   Response::success("编辑成功");
        }
        $id=input('id');
        $list=$this->model->idFind($id);
        $this->assign('list',$list);
        $this->assign('question',$list->question());
        return $this->fetch();
    }
    public function category_add(){
        if($this->request->isPost()){
            $data=input();
            $r=$this->model->cate_add($data);
            if($r===false){
                return  json(['code'=>1,'msg'=>"编辑失败"]);
            }
            return  json(['code'=>0,'msg'=>"编辑成功"]);
        }
        return $this->fetch();
    }

    public function category_edit(){
        if($this->request->isPost()){
            $data=input();
            $r=$this->model->cate_edit($data);
            if($r===false){
                return  json(['code'=>1,'msg'=>"编辑失败"]);
            }
            return  json(['code'=>0,'msg'=>"编辑成功"]);
        }
        $id=input('id',0,'intval');
        $naire=$this->model->get($id);
        $this->assign('naire',$naire);
        return $this->fetch();
    }

    public function tongji($id=0){
        list($group,$base)=$this->model->tongji($id);
        $naire=$this->model->get($id);

        $this->assign('naire',$naire);
        $this->assign('group',$group);
        $this->assign('base',$base);
        return $this->fetch();
    }

    public function tongjibak($id=0){
        list($group,$base)=$this->model->tongji($id);
        $naire=$this->model->get($id);

        $this->assign('naire',$naire);
        $this->assign('group',$group);
        $this->assign('base',$base);
        return $this->fetch();
    }
    public function parentShow(){
        $parent=input('parent','');
        $id=input('id',0,'intval');
        list($group,$base)=$this->model->parentShow($id,$parent);
        $naire=$this->model->get($id);

        $this->assign('id',$id);
        $this->assign('parent',$parent);
        $this->assign('naire',$naire);
        $this->assign('group',$group);
        $this->assign('base',$base);
        return $this->fetch();
    }
    public function childrenShow(){
        $parent=input('parent','');
        $children=input('children','');
        $id=input('id',0,'intval');
        list($group,$base)=$this->model->childrenShow($id,$parent,$children);

        $this->assign(compact('id','children','parent'));
        $naire=$this->model->get($id);
        $this->assign('naire',$naire);
        $this->assign('group',$group);
        $this->assign('base',$base);
        return $this->fetch();
    }
    public function juzhengshow(){
        $parent=input('parent','');
        $children=input('children','');
        $id=input('id',0,'intval');
        list($group,$base)=$this->model->juzhengShow($id,$parent,$children);
        $total=[];
        $eart=[];
        array_walk($group,function($v,$k)use(&$total,&$eart){
           foreach ($v['answer'] as $kk=>$vv){
               if(!isset($eart[$kk])){
                   $eart[$kk]=[];
               }
               if(!isset($eart[$kk][$v['name']])){
                   $eart[$kk][$v['name']]=$vv;
               }else{
                   foreach ($vv as $tkkk=>$tvvv){
                       $eart[$kk][$v['name']][$tkkk]+=$tvvv;
                   }
               }
               foreach ($vv as $kkk=>$vvv){
                   if(!isset($total[$kk][$kkk])){
                       $total[$kk][$kkk]=0;
                   }
                   $total[$kk][$kkk]+=$vvv;
               }
           }
        });
        $this->assign(compact('id','children','parent','total','eart'));
        $naire=$this->model->get($id);
        $this->assign('naire',$naire);
        $this->assign('group',$group);
        $this->assign('base',$base);
        return $this->fetch();
    }
    public function uploadstxt(){
        $uploads=$this->request->file('file');
        $file=$uploads->move(ROOT_PATH.'public/uploads',$uploads->getInfo('name'));
        $filePath=$file->getRealPath();
        $file=null;
        $text=new Text();
        return $text->text($filePath);
    }
}